﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DBUtility;
using System.Data.SqlClient;

namespace TierApplication
{
    /// <summary>
    /// 类T_OrderDetail。
    /// </summary>
    /// 
    public partial class T_OrderDetail
    {

        public string GetAddSql()
        {
            if (UnitPriceM == null) UnitPriceM = 0;
            if (PlnQuantity == null) PlnQuantity = 0;
            if (ShpQuantity == null) ShpQuantity = 0;
            if (ActQuantity == null) ActQuantity = 0;
            if (UnitPrice == null) UnitPrice = 0;
            if (Discount == null) Discount = 0;
            if (SumPrice == null) SumPrice = 0;
            if (Point == null) Point = 0;
            if ((RelaID==null)||(RelaID==""))
                RelaID = OrderDetailID;


            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [T_OrderDetail](");
            strSql.Append("OrderDetailID,OrderID,ProductID,ProductNo,ProductName,PlnQuantity,ShpQuantity,ActQuantity,BckQuantity,UnitPriceM,UnitPrice,Discount,SumPrice,Point,Remark,CreateDate,CreateUser,BatchNo,UsefulLifeType,UsefulLife,DepotArea,RelaID");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("'" + OrderDetailID + "',");
            strSql.Append("'" + OrderID + "',");
            strSql.Append("'" + ProductID + "',");
            strSql.Append("'" + ProductNo + "',");
            strSql.Append("'" + ProductName + "',");
            strSql.Append("" + PlnQuantity + ",");
            strSql.Append("" + ShpQuantity + ",");
            strSql.Append("" + ActQuantity + ",");
            strSql.Append("" + BckQuantity + ",");
            strSql.Append("" + UnitPriceM + ",");
            strSql.Append("" + UnitPrice + ",");
            strSql.Append("" + Discount + ",");
            strSql.Append("" + SumPrice + ",");
            strSql.Append("" + Point + ",");
            strSql.Append("'" + Remark + "',");
            strSql.Append("getdate(),");
            strSql.Append("'" + TierService.Utility.gUserID + "',");
            strSql.Append("'" + BatchNo + "',");
            strSql.Append("'" + UsefulLifeType + "',");
            if (UsefulLife == null)
            {
                strSql.Append("null,");
            }
            else
            {
                strSql.Append("'" + UsefulLife + "',");
            }
            strSql.Append("'" + DepotArea + "',");
            strSql.Append("'" + RelaID + "'");
            strSql.Append(")");

            return strSql.ToString();
        }



        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetOrderDetailList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM S_OrderDetail ");
            if (strWhere.Trim() != "")
            {

                strSql.Append(" where 1=1  " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }



        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsPOrderDetailID(string POrderDetailID, string sOrderID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [T_OrderDetail]");
            strSql.Append(" where POrderDetailID='" + POrderDetailID + "' and OrderID='" + sOrderID + "'");
            int iCount = int.Parse(DbHelperSQL.Query(strSql.ToString()).Tables[0].Rows[0][0].ToString());

            return iCount > 0;


        }

        public string GetOrderDetailRelaID(string sOrderDetailID, string sPOrderDetailID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select dbo.GetOrderDetailRelaID('" + sOrderDetailID + "','" + sPOrderDetailID + "') RelaID");
            string sRelaID = DbHelperSQL.Query(strSql.ToString()).Tables[0].Rows[0][0].ToString();

            return sRelaID;
        }




    }
}

